import React from 'react';
import './GoodsCar.less';
import {withRouter} from 'react-router-dom';
import {reqCartadd} from '../http/api';
import { successAlert } from '../utils/alert';
async  function addShopCar(e,goodsid){
  e.stopPropagation();
  let params = {
      uid:JSON.parse(localStorage.getItem('userInfo')).uid,
      type:1,
      goodsid,
      num:1
  }
  try{
      let res = await reqCartadd(params);  
      if(res.data.code === 200){
          successAlert(res.data.msg)
      }
  }catch(err){
      console.log(err);
  }
                     
}
function GoodsCar(props) {
    let goodsArr = props.goodsArr;
  return (
    <ul className='GoodsCar'>
                {goodsArr.map(item=>{
                  return <li key={item.id} onClick={()=>props.history.push(`/detail/${item.id}`)}>
                    <div className="left">
                      <img src={item.img} alt="" />
                    </div>
                    <div className="right">
                      <h3>{item.goodsname}</h3>
                      <div className="footer">
                        <div className="price">
                          <span>￥{item.price}</span>
                          <del>￥{item.market_price}</del>
                        </div>
                        <button onClick={(e)=>addShopCar(e,item.id)}><img src="/uploads/btn_normal.png" alt="" /></button>
                      </div>
                    </div>
                  </li>
                })}
              </ul>
  )
}

export default withRouter(GoodsCar);
